package com.dyh.quickdemozwei;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

/*

*/

@Slf4j
public class TestSeeZwei {

    @Setter@Getter
    class Foo{
        String foo;
    }

    @Test public void test2() throws InterruptedException {
        Foo foo = new Foo();
        foo.setFoo("a");
        log.info("{} {} {}",Thread.currentThread().getName(),System.identityHashCode(foo),
                System.identityHashCode(foo.getFoo()));
        Runnable read = ()->{
//            log.info("{} {}",Thread.currentThread().getName(),foo.hashCode());
            log.info("{} {}",Thread.currentThread().getName(),System.identityHashCode(foo));
        };

        Runnable r1 = ()->{
            log.info("{} {} {}",Thread.currentThread().getName(),System.identityHashCode(foo),
                    System.identityHashCode(foo.getFoo()));
            try {
                Thread.sleep(500L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            foo.setFoo("b");
            log.info("{} {} {}",Thread.currentThread().getName(),System.identityHashCode(foo),
                    System.identityHashCode(foo.getFoo()));
        };

        Runnable r2 = ()->{
            log.info("{} {} {}",Thread.currentThread().getName(),System.identityHashCode(foo),
                    System.identityHashCode(foo.getFoo()));            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            log.info("{} {} {}",Thread.currentThread().getName(),System.identityHashCode(foo),
                    System.identityHashCode(foo.getFoo()));
        };

        Thread t1 = new Thread(r1);
        t1.setName("t1");

        Thread t2 = new Thread(r2);
        t2.setName("t2");

        t1.start();
        t2.start();

        t1.join();
        t2.join();
    }


    ThreadPoolExecutor poolExecutor;
    {
        this.poolExecutor = new ThreadPoolExecutor(3,6,
                300, TimeUnit.SECONDS,
//                new SynchronousQueue<>(),
                new LinkedBlockingQueue<>(200),
                new ThreadFactoryBuilder().setNameFormat("pool-%d").build(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
    }

    Consumer<Integer> consumer = e->{
        log.info("{} 开始消费-{}",Thread.currentThread().getName(),e);
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        log.info("{} 结束消费-{}",Thread.currentThread().getName(),e);
    };

    @Test
    public void test() throws InterruptedException {

        List<Integer> datas = Lists.newArrayList(1,2,3,4,5);
        Integer[] tags = new Integer[datas.size()];


        for (int i = 0;i<datas.size();i++){
            int finalI = i;
            poolExecutor.submit(()->{
                consumer.accept(datas.get(finalI));
                tags[finalI]=1;
            });
        }






        Thread t2 = new Thread(()->{
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            l1:
            for (;;){
                for (int i=tags.length-1;i>=0;i--){
                    if (tags[i]==null ||tags[i]==0)
                        continue l1;
                }

                break ;
            }


        });
        t2.setName("t2");

        t2.start();

        t2.join();
    }

}
